package edu.thu.studio.ormdesigner.outline.dialog;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.thu.studio.ormdesigner.common.AbstractDialog;
import edu.thu.studio.ormdesigner.common.CompositeFactory;
import edu.thu.studio.ormdesigner.model.ORMModel;
import edu.thu.studio.ormdesigner.model.domain.Domain;
import edu.thu.studio.ormdesigner.util.Properties;
import edu.thu.studio.ormdesigner.util.DataTypeUtils;
import edu.thu.studio.ormdesigner.util.Utils;

public class DomainDialog extends AbstractDialog {
    
    private Text codeText;
    private Text nameText;
    private Text commentText;
    private Combo dataTypeCombo;
    private Text lengthText;
    private Text precisionText;
    
    private Domain result;
    private Domain domain;
    private ORMModel ormModel;
    
    public DomainDialog(Shell parentShell, Domain domain, ORMModel ormModel) {
        super(parentShell);
        this.domain = domain;
        this.ormModel = ormModel;
    }
    
    @Override
    protected void initialize(Composite composite) {
        this.codeText = CompositeFactory.createText(this, composite, Properties.outline_label_code, false, true);
        this.nameText = CompositeFactory.createText(this, composite, Properties.outline_label_domain, false, true);
        this.commentText = CompositeFactory.createTextArea(this, composite, Properties.outline_label_comment, 300, 100,
            1, true);
        this.dataTypeCombo = CompositeFactory.createReadOnlyCombo(this, composite, Properties.outline_label_datatype,
            this.getNumColumns() - 1, -1);
        this.lengthText = CompositeFactory.createNumText(this, composite, Properties.outline_label_length, true);
        this.precisionText = CompositeFactory.createNumText(this, composite, Properties.outline_label_precision, true);
    }
    
    @Override
    protected String getErrorMessage() {
        String code = this.codeText.getText().trim();
        String name = this.nameText.getText().trim();
        String combo = this.getSelectedDataType();
        String length = this.lengthText.getText().trim();
        String precision = this.precisionText.getText().trim();
        
        if (code.equals("")) {
            return Properties.error_code_empty;
        }
        
        if (name.equals("")) {
            return Properties.error_domain_name_empty;
        }
        
        // 新增
        if (null == this.domain) {
            Iterator<Domain> iter = this.ormModel.getDomains().iterator();
            while (iter.hasNext()) {
                Domain _domain = iter.next();
                if (_domain.getName().equals(name)) {
                    return Properties.error_domain_name_exist;
                }
            }
        }
        
        if (combo.equals("")) {
            return Properties.error_domain_datatype_empty;
        }
        
        List<Map<String, String>> dataTypeValidate = DataTypeUtils.getDataTypeValidate();
        String validate = null;
        for (int i = 0; i < dataTypeValidate.size(); i++) {
            Map<String, String> m = dataTypeValidate.get(i);
            if (m.containsKey(combo)) {
                validate = String.valueOf(m.get(combo));
                break;
            }
        }
        if (validate.equals("!null")) {
            if (length.equals("")) {
                return Properties.error_domain_length_empty;
            }
        }
        if (!length.equals("null")) {
            if (!length.equals("")) {
                try {
                    Integer.parseInt(length);
                } catch (NumberFormatException e) {
                    return Properties.error_domain_length_degit;
                }
            }
        }
        if (!precision.equals("null")) {
            if (!precision.equals("")) {
                try {
                    Integer.parseInt(precision);
                } catch (NumberFormatException e) {
                    return Properties.error_domain_length_degit;
                }
            }
        }
        return null;
    }
    
    @Override
    protected String getTitle() {
        return Properties.outline_label_domain;
    }
    
    protected String getSelectedDataType() {
        int index = this.dataTypeCombo.getSelectionIndex();
        if (index > -1) {
            return DataTypeUtils.getDataType()[index];
        }
        return "";
    }
    
    @Override
    protected void perfomeOK() throws Exception {
        this.result = new Domain();
        if (null != this.domain) {
            this.result.setId(this.domain.getId());
        } else {
            this.result.setId(Utils.getUUID());
        }
        this.result.setCode(this.codeText.getText().trim());
        this.result.setName(this.nameText.getText().trim());
        this.result.setComment(this.commentText.getText().trim());
        this.result.setDataType(this.getSelectedDataType());
        String length = this.lengthText.getText().trim();
        if (!length.equals("") && !length.equals("null")) {
            this.result.setLength(Integer.parseInt(length));
        }
        String precision = this.precisionText.getText().trim();
        if (!precision.equals("") && !precision.equals("null")) {
            this.result.setPrecision(Integer.parseInt(precision));
        }
    }
    
    @Override
	protected void setData() {
		this.dataTypeCombo.setItems(DataTypeUtils.getDataType());
		if (null != this.domain) {
			this.codeText.setText(this.domain.getCode());
			this.nameText.setText(this.domain.getName());
			this.commentText.setText(this.domain.getComment());
			this.dataTypeCombo.setText(this.domain.getDataType());

			if (null != this.domain.getLength()) {
				this.lengthText
						.setText(String.valueOf(this.domain.getLength()));
			}
			if (null != this.domain.getPrecision()) {
				this.precisionText.setText(String.valueOf(this.domain
						.getPrecision()));
			}
		}
	}
    
    @Override
    protected void addListener() {
        this.dataTypeCombo.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
            }
        });
    }
    
    public Domain getResult() {
        return result;
    }
    
}
